package juc;

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @author Guo
 * @Create 2020/8/19 22:00
 */
class AirConditior
{
    private int number = 0;
    private Lock lock = new ReentrantLock();
    private Condition condition = lock.newCondition();

    /**
     * await 当前线程等待
     * signal 唤醒一个等待线程
     * signalAll 唤醒所有等待线程
     * @throws InterruptedException
     */
    public  void increment() throws InterruptedException
    {
      lock.lock();
      try {
          //1、判断
          while (number != 0) {
//              this.wait();
              condition.await();
          }
          //干活
          number++;
          System.out.println(Thread.currentThread().getName() + "\t" + number);
          //3.通知
//          this.notifyAll();
          condition.signalAll();
      } catch (Exception e) {
          e.printStackTrace();
      } finally {
          lock.unlock();
      }
    }

    public  void decrement() throws InterruptedException
    {
        lock.lock();
        try {
            //1、判断
            while (number == 0) {
//              this.wait();
                condition.await();
            }
            //干活
            number--;
            System.out.println(Thread.currentThread().getName() + "\t" + number);
            //3.通知
//          this.notifyAll();
            condition.signalAll();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            lock.unlock();
        }
    }


    //老版本
 /*   public synchronized void increment() throws InterruptedException
    {
        //1、判断
        while (number != 0) {
            this.wait();
        }
        //干活
        number++;
        System.out.println(Thread.currentThread().getName() + "\t" + number);
        //3.通知
        this.notifyAll();
    }
    public synchronized void decrement() throws InterruptedException
    {
        //1、判断
        while (number == 0) {
            this.wait();
        }
        //干活
        number--;
        System.out.println(Thread.currentThread().getName() + "\t" + number);
        //3.通知
        this.notifyAll();
    }*/

}

/**
 *  题目：现在两个线程 可以操作初始值为零的一个变量值
 *  实现一个线程对改变量加1 一个线程对该变量减1
 *  实现交替 来10轮 变量初始值为0
 *
 *  1、高内聚低耦合 线程操作资源类
 *  2、判断/干活/通知
 *  3、多线程交互中，必须要防止多线成唤醒，也即(判断用while，不能用if)
 *      while被唤醒后还在循环里
 */
public class ThreadWaitNotifyDemo {
    public static void main(String[] args) {

        AirConditior airConditior = new AirConditior();
        new Thread(() ->{
            for(int i = 1; i<= 10; i++) {
                try {
                    airConditior.increment();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        },"A").start();

        new Thread(() -> {
            for(int i = 1; i<= 10; i++) {
                try {
                    airConditior.decrement();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }

        },"B").start();

        new Thread(() ->{
            for(int i = 1; i<= 10; i++) {
                try {
                    airConditior.increment();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        },"A").start();

        new Thread(() -> {
            for(int i = 1; i<= 10; i++) {
                try {
                    airConditior.decrement();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }

        },"B").start();

    }
}